<?php
    Class Solution{

        /**
         * 182. 查找重复的电子邮箱
         * @return void
         */
        function sql182()
        {
            $sql = 'select EMAIL FROM PERSON GROUP BY EMAIL
                    HAVING COUNT(email) > 1';
        }

        /**
         * 183. 从不订购的客户
         * @return void
         */
        function sql183()
        {
            $sql = 'select name as Customers from customers as a
                    where a.id not in (select customerid from orders as b)';
        }

        /**
         * 190. 颠倒二进制位
         * @param Integer $n
         * @return Integer
         */
        function reverseBits190($n) {
            $count = 0;
            $str = '';
            while ($count < 32) {
                if($n >= 0){
                    $str .= $n % 2;
                    $n = intval($n/2);
                }else{
                    $str .= '0';
                }
                $count++;
            }
            $count = 0;
            for ($i = 31; $i >= 0; $i--) {
                $count += $str[$i] * (2**(31-$i));
            }
            return $count;
        }

        /**
         * 191. 位1的个数
         * @param Integer $n
         * @return Integer
         */
        function hammingWeight191($n) {
            $count = 0;
            while ($n > 0) {
                if($n % 2){
                    $count++;
                }
                $n = intval($n/2);
            }
            return $count;
        }

        /**
         * 193. 有效电话号码
         * @return void
         */
        function shell193()
        {
            $str = "grep -E '^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$' file.txt";
        }

        /**
         * 195. 第十行
         * @return void
         */
        function shell195()
        {
            $str = "sed -n '10p' file.txt";
        }

        /**
         * 202. 快乐数
         * @param Integer $n
         * @return Boolean
         */
        function isHappy202($n) {
            $hash=[];
            $n = (string)$n;
            $count = 1;
            while($n != 1){
                $happy = 0;
                echo '$n1 = ' . $n .PHP_EOL;
                $count++;
                $n = (string)$n;
                $len = strlen($n);
                for($i = 0; $i < $len; $i++){
                    $happy += $n[$i]**2;
                }
                if(!$hash[$happy]){
                    $hash[$happy] = 1;
                }else{
                    $happy = 0;
                    return false;
                }
                if($happy == 1){
                    return true;
                }
                $n = $happy;
            }

            return true;
        }

        /**
         * 205. 同构字符串
         * @param String $s
         * @param String $t
         * @return Boolean
         */
        function isIsomorphic205($s, $t) {
            $map  = [];
            $map2 =[];
            $length = strlen($s);
            for($i = 0; $i < $length; $i++){
                if(!$map[$s[$i]] && $map[$s[$i]] !== '0'){
                    if($map2[$t[$i]]){
                        return false;
                    }
                    $map[$s[$i]] = $t[$i];
                    $map2[$t[$i]] = $s[$i];
                }
                if($map[$s[$i]] != $t[$i]){
                    return false;
                }
            }

            return true;
        }

        /**
         * 217. 存在重复元素
         * @param Integer[] $nums
         * @return Boolean
         */
        function containsDuplicate217($nums) {
            $map = [];
            foreach($nums as $num) {
                if(!$map[$num] && $map[$num] !== 0){
                    $map[$num] = $num;
                }
                else{
                    return true;
                }
            }

            return false;
        }

        /**
         * 219. 存在重复元素 II
         * @param Integer[] $nums
         * @param Integer $k
         * @return Boolean
         */
        function containsNearbyDuplicate219($nums, $k) {
            $map = [];
            $count = 0;
            foreach($nums as $num) {
                if(!$map[$num] && $map[$num] !== 0){
                    $map[$num] = $count;
                }else{
                    if(($count - $map[$num]) <= $k){
                        return true;
                    }else{
                        $map[$num] = $count;
                    }
                }
                $count++;
            }

            return false;
        }


    }
    $solution = new Solution();
$nums = [1,0,1,1];
$k=1;

    print_r($solution->containsNearbyDuplicate219($nums,$k));


?>